﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">
    <title>易得桌面</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageStyles" runat="server">
    <style type="text/css">
        .formBox
        {
            background: none repeat scroll 0 0 #E5E5E5;
            padding: 1em;
        }
        .calendar-blank
        {
            background: url("../../Content/images/calendar_blank.png") no-repeat scroll 0 0 transparent;
            cursor: pointer;
            display: inline-block;
            height: 22px;
            margin-bottom: -4px;
            margin-left: 4px;
            margin-right: 4px;
            width: 22px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PageScripts" runat="server">
    <script type="text/javascript">
    //************* URL常量

    var URL_APPLY_LEAVE = '/attendance/DoApplyLeave';

    //************* 全局变量

    var g_Var = {
        'leaveTypeDefault': '请选择请假类型...',
        'dateTimeBegin': null,
        'dateTimeEnd': null,
        'dateNow': null

    };
    
    //************* 常量定义


    //************* Accessor访问器

    if (ezmock.IS_MOCKED) {

        var mock_TypeJson = [{ "Id": "a8c3b71b-aa58-43c2-854a-1d632ee67d69", "LeaveTypeName": "其它", "IsForWork": false, "Remark": null },
                             { "Id": "06932c6a-c358-40e9-b583-21e98e4bce43", "LeaveTypeName": "病假", "IsForWork": false, "Remark": null },
                             { "Id": "3e58acf8-9c41-4df9-90ea-46c41a8a8668", "LeaveTypeName": "调休", "IsForWork": false, "Remark": "" },
                             { "Id": "6d440982-1a18-49ae-b7db-5bd9e1406da4", "LeaveTypeName": "产假", "IsForWork": false, "Remark": "" },
                             { "Id": "9c0e5dc7-a223-4531-9103-717aa68c024b", "LeaveTypeName": "事假", "IsForWork": false, "Remark": "" },
                             { "Id": "c02d31ec-91f8-451c-9013-74e386fa2997", "LeaveTypeName": "婚假", "IsForWork": false, "Remark": "" },
                             { "Id": "c7615898-2739-4c33-a8dc-a1451537b170", "LeaveTypeName": "外出", "IsForWork": true, "Remark": "在本市内外出办事的情况" },
                             { "Id": "494c6766-66f2-4831-89e8-a14937febf47", "LeaveTypeName": "年假", "IsForWork": false, "Remark": "公司规定的年假" },
                             { "Id": "5ecb8974-3890-4d48-8f05-b01bf55f5b18", "LeaveTypeName": "检假", "IsForWork": false, "Remark": "" },
                             { "Id": "2140a9d5-3149-44c5-9f2c-f6fa7f3913b5", "LeaveTypeName": "出差", "IsForWork": true, "Remark": ""}];

        var mock_CurrentTime = { 'CurrentTime': '1280423559' };

        var mockEzAccessor = {
            'doApplyLeave': function () {
                var statusInfo = { 'Status': 'Success', 'Info': '申请成功！', 'Date': null };
                $.displayStatusInfo(statusInfo);
            },
            'getUserId': function () {
                return '4d2d80d1-48cb-4661-8127-4a8e3db67760';
            },
            'getLeaveType': function () {
                return mock_TypeJson;
            },
            'getBasicInfo': function () {
                g_Var['dateNow'] = Date.fromEpoch(mock_CurrentTime['CurrentTime']);
            }
        };
        EzAccessor = mockEzAccessor;
    } else {
        var realEzAccessor = {
            'doApplyLeave': function (dateTimeBegin, dateTimeEnd, leaveTypeId, description) {
                $('.page-submit').ajaxBusyProgress();
                $('#leaveReason').attr('disabled', true);
                $.post(
                    URL_APPLY_LEAVE,
                    { 'dateTimeBegin': dateTimeBegin,
                        'dateTimeEnd': dateTimeEnd,
                        'leaveTypeId': leaveTypeId,
                        'description': description
                    },
                    function (e) {
                        var userId = EzAccessor.getUserId();
                        if (e.Status == "Success") {
                            var url = '/attendance/personalLeaveInfoList?UserId=' + userId + '&CurrentTime=' + dateTimeBegin;
                            $.redirectAndDisplayInfo(url, e);
                        } else {
                            $('.page-submit').ajaxBusyProgress('reset');
                            $.displayStatusInfo(e);
                        }
                    }
                );
            },
            'getUserId': function () {
                return '<%= ViewData["UserId"] %>';
            },
            'getLeaveType': function () {
                return <%= ViewData["LeaveTypeList"] %>;
            },

            'getBasicInfo': function () {
                g_Var['dateNow'] = Date.fromEpoch(<%= ViewData["CurrentTime"] %>);
            }
        };
        EzAccessor = realEzAccessor;
    }


    //刷新时间
    function refreshCurrentDateTime() {
        var timeBegin = $('#timeBegin').timePicker('getTime');
        var timeEnd = $('#timeEnd').timePicker('getTime');

        var dateBegin = $('#dateBegin').datepicker('getDate');
        var dateEnd = $('#dateEnd').datepicker('getDate');

        g_Var['dateTimeBegin'] = new Date(
            dateBegin.getFullYear(),
            dateBegin.getMonth(),
            dateBegin.getDate(),
            timeBegin.getHours(),
            timeBegin.getMinutes(),
            0
            );
        g_Var['dateTimeEnd'] = new Date(
            dateEnd.getFullYear(),
            dateEnd.getMonth(),
            dateEnd.getDate(),
            timeEnd.getHours(),
            timeEnd.getMinutes(),
            0
            );

    }


    function dateTimeBeginChanged() {
        refreshCurrentDateTime();

        if (g_Var['dateTimeBegin'] > g_Var['dateTimeEnd']) {
            var timeTarget = new Date(g_Var['dateTimeBegin']);

            $('#dateEnd').datepicker('setDate', timeTarget);
            $('#timeEnd').timePicker('setTime', g_Var['dateTimeBegin']);
        }
    }


    function dateTimeEndChanged(currentTime) {
        refreshCurrentDateTime(currentTime);

        if (g_Var['dateTimeBegin'] > g_Var['dateTimeEnd']) {

            var dateBegin = new Date(g_Var['dateTimeBegin']);
            $('#dateEnd').datepicker('setDate', dateBegin);
            $('#timeEnd').timePicker('setTime', g_Var['dateTimeBegin']);
            EzTipsManager.showTipBox('tipAlert');

            setTimeout(function () {
                EzTipsManager.hideTipBox('tipAlert');
            }, 3000);
        }
    }


    //初始化假期开始时间
    function initiDateBegin() {

        $('#dateBegin')
		   .wdatepicker({
		       onSelect: function (dataText, inst) {
		           dateTimeBeginChanged();
		       }
		   })
		   .val(g_Var['dateNow'].toFullDate());

        $('#timeBegin').timePicker({
            'select': function (event, currentTime) {
                dateTimeBeginChanged(currentTime)
            }
        });

        $('#timeBegin').timePicker("setTime", g_Var['dateNow']);


    }

    //初始化假期结束时间
    function initiDateEnd() {
        var endTime = new Date(
            g_Var['dateNow'].getFullYear(),
            g_Var['dateNow'].getMonth(),
            g_Var['dateNow'].getDate() + 1,
            g_Var['dateNow'].getHours(),
            g_Var['dateNow'].getMinutes(),
            0
        );

        $('#dateEnd')
		   .wdatepicker({
		       onSelect: function (dataText, inst) {
		           dateTimeEndChanged();
		       }
		   })
		   .val(endTime.toFullDate());

        $('#timeEnd').timePicker({
            'select': function (event, currentTime) {
                dateTimeEndChanged(currentTime);
            }

        });

        $('#timeEnd').timePicker("setTime", g_Var['dateNow']);
    }

    //初始化时间
    function initiTime() {
        
        EzAccessor.getBasicInfo();

        initiDateBegin();
        initiDateEnd();
    }

    //初始化页面
    function initPage() {
        EzTipsManager.addTipBox('#tip_Remind', 'tipRemind');
        EzTipsManager.addTipBox('#tip_alert', 'tipAlert');
        
        EzTipsManager.showTipBox('tipRemind');

        var leaveType = EzAccessor.getLeaveType();

        $('#selectControl').comboxBinding(leaveType, 'LeaveTypeName', 'Id');
    }

    //初始化事件
    function initPageEvent() {
        $('#btnSubmit').click(function () {

            refreshCurrentDateTime();

            var dateTimeBeginToEpoch = g_Var['dateTimeBegin'].toEpoch();
            var dateTimeEndToEpoch = g_Var['dateTimeEnd'].toEpoch();
            var leaveTypeId = $('#selectControl').val();
            var leaveDescription = $('#leaveReason').val().htmlEncode();

            EzAccessor.doApplyLeave(dateTimeBeginToEpoch, dateTimeEndToEpoch, leaveTypeId, leaveDescription);

        });

        $('#btnRecover').click(function () {
            //时间重置
            $('#timeEnd').timePicker("setTime", g_Var['dateNow']);
            $('#timeBegin').timePicker("setTime", g_Var['dateNow']);

            $('#dateBegin').val(g_Var['dateNow'].toFullDate());
            $('#dateEnd').val(g_Var['dateNow'].toFullDate());

            //假期类别
            $('#selectControl').val(g_Var['leaveTypeDefault']);

            //备注
            $('#leaveReason').clearFields();
        });


    }

    $(function () {
        initPage();
        initPageEvent();
        initiTime();

    });

    </script>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PageHead" runat="server">
    <%
        Html.RenderPartial("_sayHello"); %>
    <div id="status-bar"><% Html.RenderPartial("_navigateTo"); %></div>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="PageContent" runat="server">
    <div id="page-head">
        <img src="../../Content/icons/attendance_applyLeave.png" style="width: 48px; height: 48px;"
            alt="applyLeave" />
        <span>申请假期</span>
    </div>
    <div id="page-body">
        <div class="formBox">
            <ul class="fieldslist">
                <li>
                    <div class="l">
                        <label>
                            请假类型：
                        </label>
                    </div>
                    <div class="r">
                        <select id="selectControl" class="f-input" name="typeList" value="选择请假类别">
                        </select>
                    </div>
                </li>
                <li>
                    <div class="l">
                        <label>
                            开始时间：
                        </label>
                    </div>
                    <div class="r">
                        <input id="dateBegin" />
                        <div id="timeBegin" style="display: inline-block;">
                        </div>
                    </div>
                </li>
                <li>
                    <div class="l">
                        <label>
                            结束时间：
                        </label>
                    </div>
                    <div class="r">
                        <input id="dateEnd" />
                        <div id="timeEnd" style="display: inline-block;">
                        </div>
                    </div>
                </li>
                <li>
                    <div class="l">
                        <label>
                            请假原因：
                        </label>
                    </div>
                    <div class="r">
                        <textarea id="leaveReason" class="f-input" rows="4"></textarea>
                    </div>
                </li>
            </ul>
        </div>
    </div>
    <div class="page-submit">
        <input id="btnSubmit" type="submit" value="提交申请" />
        &nbsp;或者&nbsp;<a id="btnRecover">重置</a>
    </div>
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="PageShortcut" runat="server">
    <div id="shortcuts" class="box">
        <a id="btnPersonalLeaveInfo" class="btn-shortcut" href="/attendance/personalLeaveInfoList">
            查看假期记录</a>
    </div>
</asp:Content>
<asp:Content ID="Content7" ContentPlaceHolderID="PageFoot" runat="server">
    <div id="tip_Remind" style="display: none;">
        <h1>
            小贴士</h1>
    </div>
    <div id="tip_alert" style="display: none;" class="tip-warning">
        <h1>
            注意:</h1>
        <p class="indent">
            假期开始时间必须早于假期结束时间!</p>
    </div>
</asp:Content>
